package com.jic.order.impl;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.RestResult;
import com.jic.order.base.BaseService;
import com.jic.order.entity.OrderLogisticsCompany;
import com.jic.order.manage.request.OrderLogisticsCompanyRequest;
import com.jic.order.manage.response.ExpressMapConfigResponse;
import com.jic.order.mapper.OrderLogisticsCompanyMapper;
import com.jic.common.base.vo.PageResult;
import com.jic.order.service.OrderLogisticsCompanyService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-01-17 05:43:58
 */
@Service
public class OrderLogisticsCompanyServiceImpl extends BaseService implements OrderLogisticsCompanyService {

	@Autowired
	private OrderLogisticsCompanyMapper orderLogisticsCompanyMapper;

	@Override
	public RestResult insert(OrderLogisticsCompany orderLogisticsCompany) {
		List<OrderLogisticsCompany> nameList = orderLogisticsCompanyMapper.queryByLogisticsCompanyName(orderLogisticsCompany.getLogisticsCompanyName());
		if (nameList.size() != 0) {
			return RestResult.error("9999", "存在相同的名称");
		}
		List<OrderLogisticsCompany> noList = orderLogisticsCompanyMapper.queryByLogisticsCompanyNo(orderLogisticsCompany.getLogisticsCompanyNo());
		if (noList.size() != 0) {
			return RestResult.error("9999", "存在相同的编码");
		}
		orderLogisticsCompanyMapper.insert(orderLogisticsCompany);
		return RestResult.success(true);

	}

	@Override
	public RestResult delete(OrderLogisticsCompanyRequest orderLogisticsCompanyRequest) {
		String[] ids = orderLogisticsCompanyRequest.getIds().split(",");
		for (int i = 0; i < ids.length; i++) {
			orderLogisticsCompanyMapper.delete(Long.valueOf(ids[i]));
		}
		return RestResult.success(true);
	}

	@Override
	public RestResult update(OrderLogisticsCompany orderLogisticsCompany) {
		OrderLogisticsCompany orderLogisticsCompanyById = orderLogisticsCompanyMapper.queryByPrimaryKey(orderLogisticsCompany.getId());
		boolean falgName = orderLogisticsCompanyById.getLogisticsCompanyName().equals(orderLogisticsCompany.getLogisticsCompanyName());
		if (!falgName) {
			List<OrderLogisticsCompany> nameList = orderLogisticsCompanyMapper.queryByLogisticsCompanyName(orderLogisticsCompany.getLogisticsCompanyName());
			if (nameList.size() != 0) {
				return RestResult.error("9999", "存在相同的名称");
			}
		}
		boolean falgNo = orderLogisticsCompanyById.getLogisticsCompanyNo().equals(orderLogisticsCompany.getLogisticsCompanyNo());
		if (!falgNo) {
			List<OrderLogisticsCompany> noList = orderLogisticsCompanyMapper.queryByLogisticsCompanyNo(orderLogisticsCompany.getLogisticsCompanyNo());
			if (noList.size() != 0) {
				return RestResult.error("9999", "存在相同编码");
			}
		}
		orderLogisticsCompanyMapper.update(orderLogisticsCompany);
		return RestResult.success(true);
	}

	@Override
	public OrderLogisticsCompany queryByPrimaryKey(Long id) {
		return orderLogisticsCompanyMapper.queryByPrimaryKey(id);
	}

	@Override
	public List<OrderLogisticsCompany> query(OrderLogisticsCompany orderLogisticsCompany) {
		return orderLogisticsCompanyMapper.query(orderLogisticsCompany);
	}

	@Override
	public PageResult<OrderLogisticsCompany> queryPage(OrderLogisticsCompany orderLogisticsCompany, Page pageQuery) {
		//计算下标
		int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
		List list = orderLogisticsCompanyMapper.queryPage(startIndex, pageQuery.getLimit(), orderLogisticsCompany);
		long count = orderLogisticsCompanyMapper.queryPageCount(orderLogisticsCompany);
		PageResult pageResult = new PageResult();
		pageResult.setRows(list);
		pageResult.setTotal(count);
		return pageResult;
	}

	@Override
	public List<ExpressMapConfigResponse> queryExpressMapConfig() {
		return orderLogisticsCompanyMapper.queryExpressMapConfig();
	}
}